<%@ page import="server.SettingServer" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page import="util.Decode" %>
<%@ page import="server.FileServer" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="config.ApplicationConfig" %>
<%@ page import="util.PlayNoticeAudio" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
    request.setCharacterEncoding(pageConfig.get("encode"));
    response.setCharacterEncoding(pageConfig.get("encode"));
    Object user = session.getAttribute("user");
    if (PlayNoticeAudio.getPlayNoticeAudio() == null) {
        PlayNoticeAudio playNoticeAudio = PlayNoticeAudio.newInstance();
        Thread thread = new Thread(playNoticeAudio);
        thread.start();
    }
%>
<html>
<head>
    <title>首 页</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/page.css">
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<nav class="navbar navbar-inverse head" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#navbar">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <%
                SettingServer settingServer = new SettingServer();
                settingServer.open();
                JSONObject logo = settingServer.readSettingObject("logo");
                if (logo.getString("logo").equals("")) {
            %>
            <img class="navbar-brand" src="image/logo.png">
            <%
            } else {
                FileServer fileServer = new FileServer();
                fileServer.open();
            %>
            <img class="navbar-brand" src="<%=fileServer.getImage(logo.getString("logo"))%>">
            <%
                    fileServer.close();
                }
            %>
            <a class="navbar-brand" href="#"><%=logo.getString("name")%>
            </a>
        </div>
        <div class="collapse navbar-collapse" id="navbar">
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="index.jsp">首 页</a>
                </li>
                <li>
                    <a href="game.jsp">比 赛</a>
                </li>
                <li>
                    <a href="rank.jsp">排 名</a>
                </li>
                <li>
                    <a href="about.jsp">通 知</a>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right text-center">
                <%
                    if (user == null || user.toString().equals("")) {
                %>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-in" data-toggle="modal" data-target="#login">&nbsp;登录</a>
                    <div class="modal fade" id="login" role="dialog" aria-hidden="true"
                         aria-labelledby="loginLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('login_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="loginLabel">登 录</h4>
                                </div>
                                <div class="modal-body">
                                    <form id="login_form" action="login" method="post">
                                        <input type="text" name="return" hidden="hidden" value="index.jsp">
                                        用户名:&nbsp;&nbsp;<input type="text" name="username">
                                        <br>
                                        <br>
                                        密&nbsp;&nbsp;&nbsp;码:&nbsp;&nbsp;<input type="password" name="password">
                                        <br>
                                        <br>
                                        验证码:&nbsp;&nbsp;<input type="text" name="ver_code" class="ver_text">
                                        <img src="ver_code" class="ver_img" id="login_ver_code_img"
                                             onclick="reload_code(document.getElementById('login_ver_code_img'))">
                                        <br>
                                        <br>
                                        <font color="red">验证码不区分大小写，点击图片刷新</font>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger"
                                            onclick="form_clear(document.getElementById('login_form'))">重 填
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('login_form'))">登 录
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-user" data-toggle="modal" data-target="#register"> 注
                        册</a>
                    <div class="modal fade" id="register" role="dialog" aria-hidden="true"
                         aria-labelledby="registerLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('register_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="registerLabel">注 册</h4>
                                </div>
                                <div class="modal-body">
                                    <form id="register_form" action="register" method="post">
                                        <input type="text" name="return" hidden="hidden" value="index.jsp">
                                        用&nbsp;&nbsp;户&nbsp;&nbsp;名:&nbsp;&nbsp;<input type="text" name="username">
                                        <br>
                                        <br>
                                        密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;&nbsp;<input
                                            type="password"
                                            name="password">
                                        <br>
                                        <br>
                                        再次输入:&nbsp;&nbsp;<input type="password" name="again">
                                        <br>
                                        <br>
                                        验&nbsp;&nbsp;证&nbsp;&nbsp;码:&nbsp;&nbsp;<input type="text" name="ver_code"
                                                                                       class="ver_text">
                                        <img src="ver_code" class="ver_img" id="register_ver_code_img"
                                             onclick="reload_code(document.getElementById('register_ver_code_img'))">
                                        <br>
                                        <br>
                                        <font color="red">更多个人信息请在注册成功后及时进行更新</font>
                                        <br>
                                        <br>
                                        <font color="red">验证码不区分大小写，点击图片刷新</font>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-danger"
                                            onclick="input_clear(document.getElementById('register_form'))">重 填
                                    </button>
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('register_form'))">注 册
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                } else {
                %>
                <li>
                    <a href="mine" class="glyphicon glyphicon-user">&nbsp;<%=user%>
                    </a>
                </li>
                <li>
                    <a href="#" class="glyphicon glyphicon-log-out" data-toggle="modal"
                       data-target="#logout">&nbsp;退出</a>
                    <div class="modal fade" id="logout" role="dialog" aria-hidden="true"
                         aria-labelledby="logoutLabel">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"
                                            onclick="input_clear(document.getElementById('logout_form'))">&times;
                                    </button>
                                    <h4 class="modal-title" id="logoutLabel">退 出 登 录</h4>
                                </div>
                                <div class="modal-body">
                                    确定要退出吗？
                                    <form id="logout_form" action="logout" method="post">
                                        <input type="text" name="username" hidden="hidden" value="<%=user%>">
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-primary"
                                            onclick="form_submit(document.getElementById('logout_form'))">&nbsp;退出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
                <%
                    }
                %>
            </ul>
        </div>
    </div>
</nav>
<%
    JSONArray carousel = settingServer.readSettingArray("carousel");
    int size = carousel.size();
%>
<div class="container main text-center">
    <div class="panel panel-primary">
        <div class="panel-body">
            <div id="myCarousel" class="carousel slide">
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                    <%
                        for (int i = 1; i < size; i++) {
                    %>
                    <li data-target="#myCarousel" data-slide-to="<%=i%>"></li>
                    <%
                        }
                    %>
                </ol>
                <div class="carousel-inner index_carousel">
                    <%
                        if (size != 0) {
                            FileServer fileServer = new FileServer();
                            fileServer.open();
                            JSONObject item = carousel.getJSONObject(0);
                    %>
                    <div class="item active">
                        <img src="<%=fileServer.getImage(item.getString("uuid"))%>"
                             alt="<%=item.getString("name")%>" class="index_img">
                    </div>
                    <%
                        for (int i = 1; i < size; i++) {
                            item = carousel.getJSONObject(i);
                    %>
                    <div class="item">
                        <img src="<%=fileServer.getImage(item.getString("uuid"))%>"
                             alt="<%=item.getString("name")%>" class="index_img">
                    </div>
                    <%
                        }
                        fileServer.close();
                    } else {
                    %>
                    <div class="item active">
                        <img src="image/index.png" alt="First slide" class="index_img">
                    </div>
                    <%
                        }
                    %>
                </div>
                <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
    </div>
    <div class="panel panel-primary">
        <div class="panel-heading"><h2 class="panel-title">提交动态</h2></div>
        <div class="panel-body">
            <iframe onload="this.height=this.contentWindow.document.body.scrollHeight" src="dynamic.jsp" frameborder="0"
                    width="100%"></iframe>
        </div>
    </div>
</div>

<%
    Decode decode = new Decode();
    JSONArray foot = settingServer.readSettingArray("foot");
    JSONObject foot0 = foot.getJSONObject(0);
    JSONObject foot1 = foot.getJSONObject(1);
    JSONObject foot2 = foot.getJSONObject(2);
%>
<div class="foot">
    <row>
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot0.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot0.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot0.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot0.getString("line2"))%>
                </p>
            </div>
        </div>
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot1.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot1.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot1.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot1.getString("line2"))%>
                </p>
            </div>
        </div>
        <div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
            <br>
            <div class="foot_item_title">
                <p><%=decode.utf8Decode(foot2.getString("title"))%>
                </p>
            </div>
            <hr class="foot_split">
            <div class="foot_item">
                <p><%=decode.utf8Decode(foot2.getString("line0"))%>
                </p>
                <p><%=decode.utf8Decode(foot2.getString("line1"))%>
                </p>
                <p><%=decode.utf8Decode(foot2.getString("line2"))%>
                </p>
            </div>
        </div>
    </row>
</div>
<%
    settingServer.close();
%>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/page.js"></script>
<script src="js/ver_code.js"></script>
<script src="js/index.js"></script>
</html>